import axios from 'axios'
// var qs = require('qs')
const host = 'http://tft-dev/api'

// -----------------------------上传模块------------------------------
// 上传文件接口 file
export function uploadFile (data) {
  const url = `http://tft-dev/api3/upload/file`
  const config = {
    headers: {'Content-Type': 'multipart/form-data'}
  }
  return axios.post(url, data, config).then(res => {
    return Promise.resolve(res.data)
  })
}

// -----------------------------产品模块------------------------------
// 查询产品
export function selectProduct (data) {
  const url = `${host}/product/products`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 创建产品
export function createProduct (data) {
  const url = `${host}/product/create`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 产品编辑页：根据id查询产品
export function selectModifProduct (id) {
  const url = `${host}/product/product/${id}`
  return axios.post(url).then(res => {
    return Promise.resolve(res.data)
  })
}

// 编辑产品
export function modifProduct (data) {
  const url = `${host}/product/modif`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 操作列：修改产品状态
export function changeStatus (data) {
  const url = `${host}/product/operation`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// -----------------------------产品规格模块------------------------------
// 根据产品id查询所有规格，规格值
export function selectProductSpec (productId ) {
  const url = `${host}/productSpec/${productId}/query`
  return axios.post(url, {}).then(res => {
    return Promise.resolve(res.data)
  })
}

// 根据产品id批量新增规格以及规格值
export function addProductSpec (productId, data) {
  const url = `${host}/productSpec/${productId}/addMany`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 根据产品id批量编辑新增，修改规格以及规格值
// 返回200时表示所有操作成功，返回30206时，表示有不可更改值，除不可更改值，其余操作成功
export function updateProductSpec (productId , data) {
  const url = `${host}/productSpec/${productId}/manage`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// -----------------------------分类模块-----------------------------
// 查询一级分类
export function selectOneType () {
  const url = `${host}/category/categorys`
  return axios.get(url).then(res => {
    return Promise.resolve(res.data)
  })
}
// 查询二三级分类
export function selectOtherType (pid) {
  const url = `${host}/category/category/${pid}`
  return axios.get(url).then(res => {
    return Promise.resolve(res.data)
  })
}
// --------------------------sku 模块-----------------------------
// 查询产品所有sku
export function getSkuList (id) {
  const url = `${host}/commodity/commoditys/${id}`
  return axios.post(url).then(res => {
    return Promise.resolve(res.data)
  })
}
// 根据sku 的id查询详情
export function getSkuDetail (id) {
  const url = `${host}/commodity/commodity/${id}`
  return axios.post(url).then(res => {
    return Promise.resolve(res.data)
  })
}
// 修改sku详情
export function editSkuDetail (data) {
  const url = `${host}/commodity/modif`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}
// 修改sku详情
export function editSkuPrice (data) {
  const url = `${host}/commodityPrice/modif`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}
// 查询所有价格类型
export function selectPriceType () {
  const url = `${host}/prices`
  return axios.get(url).then(res => {
    return Promise.resolve(res.data)
  })
}

// 添加sku价格类型
export function addSkuPrice (data) {
  const url = `${host}/commodityPrice/create`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 查询当前sku所有价格列表
export function selectSkuList (data) {
  const url = `${host}/commodityPrice/price`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}
// 生成sku
export function createSku (data) {
  const url = `${host}/commodity/create`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}
// 修改sku的上下架及可用状态
export function changeSkuState (data) {
  const url = `${host}/commodity/operation`
  return axios.post(url, data).then(res => {
    return Promise.resolve(res.data)
  })
}

// 编辑sku价格
export function selectProAllsku (id) {
  const url = `${host}/commodity/commoditys/${id}`
  return axios.post(url).then(res => {
    return Promise.resolve(res.data)
  })
}
